:: ---------------------------------------------------------------------------------------
:: @title   project generator
:: @author  LuYao
:: @date    2024/03/09
:: @encode  GBK
:: @description     
::   1 generate project
::
:: ---------------------------------------------------------------------------------------


:: @echo off: close command print
:: rem and :: comment
@echo off & setlocal EnableDelayedExpansion

:: current directory
set PRO_PATH=!cd!

:: restore point
:loop_start
cd !PRO_PATH!

@echo. 
@echo. 
@echo current path: !cd!                                                                
@echo -----------------------------------------------------------------------------------
@echo        project generator                                 
@echo -----------------------------------------------------------------------------------
@echo  code    project                                 code      project
@echo [031] jlcsky031_blink                           [091] jlcsky091_usart       
@echo [032] jlcsky032_waterlight(omit)                [092] jlcsky092_usart_recv           
@echo [033] jlcsky033_buzzer(omit)                    [093] jlcsky093_usart_hex(omit)                   
@echo [034] jlcsky034_keyled                          [094] jlcsky094_usart_txt(omit)               
@echo [035] jlcsky035_ldr(omit)                       [101] jlcsky101_i2c_mpu6050                           
@echo [041] jlcsky041_oled                            [102] jlcsky102_i2c_ina226                       
@echo [042] jlcsky042_oled_zh_utf8                    [103] jlcsky103_i2c_24cxx                   
@echo [043] jlcsky043_oled_zh_gbk                     [103] jlcsky103_i2c_24cxx                   
@echo [051] jlcsky051_exti                            [111] jlcsky111_spi_w25qxx                       
@echo [061] jlcsky061_timer_it                        [112] jlcsky112_spi_nrf24l01                       
@echo [062] jlcsky062_timer_externalclk               [113] jlcsky113_spi_tftlcd                           
@echo [063] jlcsky063_timer_pwmled                    [121] jlcsky121_bkp                           
@echo [064] jlcsky064_timer_pwmservo(omit)            [122] jlcsky122_rtc                                   
@echo [065] jlcsky065_timer_pwmmotor                  [131] jlcsky131_systemclk                                       
@echo [066] jlcsky066_timer_inputcapture1             [132] jlcsky132_mode_sleep                                       
@echo [067] jlcsky067_timer_inputcapture2(omit)       [133] jlcsky133_mode_stop                                       
@echo [068] jlcsky068_timer_encoderspeed              [134] jlcsky134_mode_standby                                   
@echo [071] jlcsky071_adc_singlechannel               [135] jlcsky135_mode_shutdown(omit)               
@echo [072] jlcsky072_adc_multichannel                [141] jlcsky141_iwdg                           
@echo [080] jlcsky080_memory(omit)                    [142] jlcsky142_wwdg                           
@echo [081] jlcsky081_dma                             [151] jlcsky151_flash(omit)
@echo [082] jlcsky082_dma_adc                     
@echo.
@echo [1] generate all     [2] clean all    [0] exit
@echo.

set omit=032,033,035,064,067,080,093,094,135,151

set /p option=Input the code:
if 0 == !option! (
    goto :loop_exit
)

if 1 == !option! (
    FOR /F %%j IN ('progen list projects') DO (
        echo --------------- [%%j - cmake         ] -------------------
        progen generate -t cmake          -v -p %%j
        echo.
        
        echo --------------- [%%j - cmake_armclang] -------------------
        progen generate -t cmake_armclang -v -p %%j
        echo.
        
        echo --------------- [%%j - uvision5_armc6] -------------------
        progen generate -t uvision5_armc6 -v -p %%j
        echo.
    )

    goto :loop_exit
)

if 2 == !option! (
    progen clean -t cmake
    progen clean -t cmake_armclang
    progen clean -t uvision5_armc6

    goto :loop_start
)

set project=!option!
:loop_split_option
FOR /f "tokens=1,* delims= " %%a IN ("!project!") DO (
    set opt=%%a
    set opt_sub=%%b

    :: Skip generating this project if it is marked as omitted.
    set omit_tmp=!omit!
    :loop_omit
    FOR /f "tokens=1,* delims=," %%m IN ("!omit_tmp!") DO (
        if "%%m" equ "!opt!" ( 
            echo Skip generating the %%m because it is marked as omitted.
            echo.
            set project=!opt_sub!
            goto loop_split_option
        )

        set omit_tmp=%%n
        goto loop_omit
    )
    
    FOR /F %%j IN ('progen list projects') DO (
        @echo %%j | findstr "!opt!" > nul && (
            echo --------------- [%%j - cmake         ] -------------------
            progen generate -t cmake          -v -p %%j
            echo.
            
            echo --------------- [%%j - cmake_armclang] -------------------
            progen generate -t cmake_armclang -v -p %%j
            echo.
            
            echo --------------- [%%j - uvision5_armc6] -------------------
            progen generate -t uvision5_armc6 -v -p %%j
            echo.
            
            set project=!opt_sub!
            goto loop_split_option
        )
    )

    set project=!opt_sub!
    goto loop_split_option
)

goto :loop_start



:loop_exit